<template>
	<view class="login-content">
		<view class="login">
			<view class="login-item login_phone">
				手机号登录1
			</view>
			<!-- #ifdef MP-WEIXIN -->
			<!-- <view class="login-item login_wx" @click="loginWx"> 微信登录 </view> -->
			<view class="login-item login_wx">
				<button open-type="getUserInfo" @getuserinfo="loginWx">
					微信登录
				</button>
			</view>
			<!-- #endif -->
			<view class="login-item login_wx">
				<button open-type="getUserInfo" @getuserinfo="loginWx">
					微信登录
				</button>
			</view>
			<view>
				<button open-type="getUserInfo" @getuserinfo="mpWxGetUserInfo">
					微信账号登录（可以弹出授权页面）
				</button>
			</view>
			<view class="login-item login_vcode" @click="loginVcode"> 短信验证码登录 </view>
		</view>
		
		<view class="login-footer">
			登录即代表您同意我们的服务协议和隐私政策
			<view>
				添加邀请码
			</view>
		</view>
		<uni-popup ref="popup" type="bottom">
			<view class="pop-vcode">
				<view class="pop-title">
					短信验证码登录
					<view>
						{{msg}}
					</view>
				</view>
				<view>
					<view class="pop-vcode-item">
						<input type="text" placeholder="注册或绑定手机号">
					</view>
					<view class="pop-vcode-item">
						<input type="text" placeholder="动态码">
					</view>
				</view>
				<view class="vcode-login">登 录</view>
			</view>
		</uni-popup>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				msg:'',//请输入手机号和动态码
			}
		},
		onLoad() {
			// if (uni.getStorageSync('uni_id_token_expired') > Date.now()) {
			// 	uni.switchTab({
			// 		url: '/pages/mine/mine'
			// 	})
			// }
		},
		methods: {
			loginWx(e){
				if(!e.detail.userInfo){
					return;
				}
				uni.showLoading({
					title: '登录中...',
					mask: true
				})
				new Promise((resolve, reject)=>{
					uni.getProvider({
						service: 'oauth',
						success(res) {
							resolve(res.provider)
						},
						fail(err) {
							reject(new Error(err.errMsg))
						}
					})
				}).then((provider)=>{
					return new Promise((resolve, reject) => {
						uni.login({
							provider,
							success: (res) => {
								resolve({
									code: res.code,
									provider
								})
							},
							fail: (err) => {
								reject(new Error(err.errMsg))
							}
						})
					})
				}).then(({
					code,
					provider
				}) => {
					return uniCloud.callFunction({
						name: 'user-center',
						data: {
							action: provider.indexOf('weixin') > -1  ? 'loginByWeixin' : 'loginByAlipay',
							params: {
								code
							}
						}
					})
				})
				.then( (res) => {
					if(res.result.type == "register"){
						this.update(res.result.uid,e)
					}
					this.$store.commit('setUid',res.result.uid)
					if (res.result.code === 0) {
						uni.hideLoading()
						this.$store.commit('setToken',{
							...res.result,
							flag:false
						})
						uni.switchTab({
							url: '/pages/mine/mine'
						})
					} else {
						throw new Error(res.result.msg)
					}
				}).catch((err) => {
					uni.hideLoading()
					uni.showModal({
						content: err.message || '登录失败',
						showCancel: false
					})
				})
			},
			loginVcode(){
				this.$refs.popup.open()
			},
			async update(uid,e){
				const [err, userData] = await uni.getUserInfo();
				const user = userData.userInfo || e.detail.userInfo;
				const res = await uniCloud.callFunction({
					name: 'user-center',
					data: {
						action: "updateUser",
						params: {
							uid,
							...user,
							avatar:user.avatarUrl
						}
					}
				})
				this.$store.commit('setUserInfo',Object.assign({avatar:user.avatarUrl},user))
			}
		}
	}
</script>

<style>
	.login-content{
		height: calc(100vh - 44px);
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		box-sizing: border-box;
	}
	.login{
		padding: 20rpx;
		text-align: center;
		box-sizing: border-box;
	}
	.login-item{
		margin: 20rpx;
		border-radius: 10rpx;
		line-height: 90rpx;
		height: 90rpx;
		font-size: 1rem;
		color: #fff;
		border: 1px solid #ccc;
		box-sizing: border-box;
	}
	.login_phone{
		background: #007AFF;
	}
	.login_wx{
		background-color: #1aad19;
	}
	.login_wx button{
		background-color: #1aad19;
		border: none;
		height: 100%;
		line-height: 90rpx;
	}
	.login_vcode{
		color: #949494;
	}
	.login-footer{
		text-align: center;
		padding-bottom: 20rpx;
		color: #cbcbcb;
	}
	.pop-vcode{
		background-color: #fff;
		padding: 20rpx;
	}
	.pop-title{
		text-align: center;
	}
	.pop-vcode-item{
		margin: 20rpx;
		background-color: #d8d8d8;
	}
	.pop-vcode-item input{
		padding: 20rpx;
	}
	.vcode-login{
		color: #fff;
		background-color: #DF5000;
		border-radius: 10rpx;
		padding: 10rpx;
		text-align: center;
	}
</style>
